﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for PostingDAO
/// </summary>
public class PostingDAO:DBAccess
{
	public PostingDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<PostingInfo> GetAll(string lang)
    {
        List<PostingInfo> list = new List<PostingInfo>();
     //   try
     //   {
            SqlCommand command = new SqlCommand("Select * From Postings Where lang=@lang Order By PostingDate", conn);
            command.Parameters.AddWithValue("@lang", lang);
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    PostingInfo post = new PostingInfo();
                    post.PostingID = dr.GetInt64(0);
                    post.PostingDate = dr.GetDateTime(1);
                    post.Title = dr.GetString(2);
                    post.Summary = dr.GetString(3);
                    post.Content = dr.GetString(4);
                    post.Lang = dr.GetString(5);
                    list.Add(post);
                }
                Disconnect();
            }
      //  }
     //   catch (SqlException ex) { }
     //   catch (Exception ex) { }
        return list;
    }
    public bool Insert(PostingInfo posting)
    {
        try
        {
            SqlCommand command = new SqlCommand("insert into Postings values(@PostDate, @Title, @Summary, @Content, @lang)", conn);
            command.Parameters.AddWithValue("@PostDate", posting.PostingDate);
            command.Parameters.AddWithValue("@Title", posting.Title);
            command.Parameters.AddWithValue("@Summary", posting.Summary);
            command.Parameters.AddWithValue("@Content", posting.Content);
            command.Parameters.AddWithValue("@lang", posting.Lang);
            //(PostDate, Title, Summary, Content, lang)


            if (Connect())
            {
                if (command.ExecuteNonQuery() > 0)
                {
                    Disconnect();
                    return true;
                }
            }
        }  
        catch (SqlException ex) { }
        catch (Exception ex) { }
        return false;
    }
}